import { Matrix4, Vector2, Matrix3, Vector3, Vector4, Color } from "@nirvana/math";
import { WebGLCanvas } from "@nirvana/canvas";
import { Camera, ParticleSet } from "@nirvana/core";

import { ParticleSystem } from "@nirvana/particles";

async function main() {
  // 使用使用 Canvas
  let canvas = document.getElementById("engine-nirvana") as HTMLCanvasElement;
  const webCanvas = new WebGLCanvas(canvas, {});
  const engine = webCanvas.engine;
  const scene = engine.sceneManager.createScene("root");

  const camera = new Camera("root camera", engine);
  scene.addCamera(camera);
  camera.transform.setPosition(0, 0, 10);

  const gl = engine.gl;

  const psys = new ParticleSystem("smoke-1");
  psys.aspectratio = camera.aspect;
  psys.setParticleQuota(120);
  psys.setEmittedEmitterQuota(6);
  psys.defaultWidth = 0.15;
  psys.defaultHeight = 0.15;
  psys.isEmitting = true;
  // psys.material = "/dev-static/textures/clover.jpg";
  // psys.material = "/dev-static/textures/light.png";
  psys.material = "/dev-static/textures/flare.png";
  psys.stacks = 1;
  psys.slices = 3;

  const boxEmitter = psys.addEmitter("Box");
  boxEmitter.name = "fragment";
  // boxEmitter.emittedEmitter = "explosionnew1";
  boxEmitter.position.cloneFrom(new Vector3(-1.5, 0.0, 0));
  boxEmitter.enabled = true;
  boxEmitter.emitted = true;
  boxEmitter.setTimeToLive(0.3, 10);
  boxEmitter.setRepeatDelay(3.5, 3.5);
  boxEmitter.setSize(4, 0, 1);
  boxEmitter.emissionRate = 25;
  boxEmitter.xRangeFactor = 0;
  boxEmitter.yRangeFactor = 0.0;
  boxEmitter.zRangeFactor = 0.1;
  boxEmitter.setDirection(new Vector3(1, 0, 0));
  boxEmitter.setMinTexcoordIndex(0);
  boxEmitter.setMaxTexcoordIndex(3);

  // const linearForceAffector = psys.addAffector("LinearForce");
  // linearForceAffector.mForceVector.y = 0.1;
  // // linearForceAffector.mForceVector.x = 0;
  // linearForceAffector.mForceApplication = "FA_ADD";

  // const colourFaderAffector = psys.addAffector("ColourFader");
  // colourFaderAffector.setAdjust(1, 0, 0, 0.0);

  const colourInterpolator = psys.addAffector("ColourInterpolator");
  colourInterpolator.setTimeAdjust(0, 0);
  colourInterpolator.setTimeAdjust(1, 0.2);
  colourInterpolator.setTimeAdjust(2, 1);
  colourInterpolator.setColourAdjust(0, new Color(1, 1, 1, 0));
  colourInterpolator.setColourAdjust(1, new Color(1, 1, 1, 1));
  colourInterpolator.setColourAdjust(2, new Color(1, 1, 1, 0));

  // const colourFader = psys.addAffector("Rotation");
  // colourFader.setAdjust1(-0.1, -0.1, -0.1, 0);
  // colourFader.setAdjust2(-0.1, -0.1, -0.1, 0);

  const particleMesh = new ParticleSet(psys);
  scene.addEntity(particleMesh);

  // engine.update = () => {
  //   console.log("update");
  //   // psys.transform.
  // };
}

main();
